//
//  DSFImageScrollView.m
//  scrollview2
//
//  Created by student on 16/2/29.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "DSFImageScrollView.h"
@interface DSFImageScrollView()

@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;

@property (weak, nonatomic) IBOutlet UIPageControl *pagecontrol;

@end
@implementation DSFImageScrollView
+(instancetype)imagescrollview
{
    return [[NSBundle mainBundle] loadNibNamed:@"DSFImageScrollView" owner:nil options:nil][0];
}
-(void)setImagenames:(NSArray *)imagenames
{
    _imagenames=imagenames;
    CGFloat imgwidth=self.scrollview.bounds.size.width;
    CGFloat imgheight=self.scrollview.bounds.size.height;
    for(int i=0;i<imagenames.count;i++)
    {
        UIImageView *imgview=[[UIImageView alloc]initWithFrame:CGRectMake(imgwidth*i, 0, imgwidth, imgheight)];
        imgview.image=[UIImage imageNamed:imagenames[i]];
        [self.scrollview addSubview:imgview];
    }
    self.scrollview.contentSize=CGSizeMake(imgwidth*self.imagenames.count, 0 );
    self.pagecontrol.numberOfPages=imagenames.count;
}
-(void)setOthercolor:(UIColor *)othercolor
{
    _othercolor=othercolor;
    self.pagecontrol.pageIndicatorTintColor=othercolor;
}
-(void)setCurcolor:(UIColor *)curcolor
{
    _curcolor=curcolor;
    self.pagecontrol.currentPageIndicatorTintColor=curcolor;
}
//-(id)initWithFrame:(CGRect)frame
//{
//    self=[super initWithFrame:frame];
//    
//    return self;
//}
-(void)layoutSubviews
{
    [super layoutSubviews];
     self.scrollview.frame = self.bounds;
    CGFloat imgwidth=self.scrollview.bounds.size.width;
    CGFloat imgheight=self.scrollview.bounds.size.height;
    for(int i=0;i<self.imagenames.count;i++)
    {
        UIImageView *imgview=self.scrollview.subviews[i];
        imgview.frame=CGRectMake(imgwidth*i, 0, imgwidth, imgheight);
    }
    self.scrollview.contentSize=CGSizeMake(imgwidth*self.imagenames.count, 0);
    CGFloat pageW=100;
    CGFloat pageH=20;
    CGFloat pageX=self.bounds.size.width/2-pageW/2;
    CGFloat pageY=self.bounds.size.height-pageH;
    self.pagecontrol.frame=CGRectMake(pageX, pageY, pageW, pageH);
    [self addSubview:self.pagecontrol];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.pagecontrol.currentPage=scrollView.contentOffset.x/400;
}

@end
